
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  defaultproperty;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import pool.tablename.Pool;
import property.Property;

import utility.ConvertToLower;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;

public class Transaction implements Defaultproperty 
{
	Pool masterobject;
	GetIdName gid;
	Hashtable<Object, Object> TransactionMap;
	Hashtable<Object, Object> table =new Hashtable<Object, Object>();
	Hashtable<Object, Object > property=new Hashtable<Object, Object>();
	String TID="tid";
	String VALUEOBJECT="valueobject"; 
	String TYPE="type";
	RecordSearch sr;
	String TABLENAME="Transaction";
	PoolLogger pl;
	
	public Transaction() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public void getResult(String value)
	{
		try 
		{
			Hashtable<Object, Object > condition=new Hashtable<Object, Object>();
			condition.put("mid", value);
			condition.put("ref",TABLENAME);
			condition.put("td", "null");
			sr.setConditionMap(condition);
			String result[] = sr.getArrayFromResultSet();
			condition.clear();
			if(TransactionMap.containsKey("id"))
			{
				condition.put("id", TransactionMap.get("id"));
				ResultSet rs  = DBManager.getselect(condition,"transaction");
				Vector<Object>  tablename = DBManager.getSelect("select * from transaction",'0');
				while(rs.next())
				{
					for(int i=0;i<tablename.size();i++)
						property.put(tablename.get(i), rs.getString(tablename.get(i).toString()));						
				}
			}	
			for(int i=0;i<result.length;i++)
			{
				String splitresult[] = result[i].split("\t");
				Property propertyobje = new Property(); 
				propertyobje.setMid(splitresult[Column.pv_index-1]);
				propertyobje.setPid(TYPE);
				String type = propertyobje.getValue();
				
				String property = gid.getItem(splitresult[Column.pid_index-1]);
				if(!property.equalsIgnoreCase("type"))
				{	
					if(!type.equalsIgnoreCase(VALUEOBJECT))
					{
						String pv = splitresult[Column.pv_index-1];
						if(pv.equals("0"))
							pv = splitresult[Column.vt_index-1];
						else
							pv = gid.getItem(pv);
						if(!TransactionMap.containsKey(property))
							TransactionMap.put(property, pv);
					}
					else
					{	
						condition.clear();
						condition.put("mid", splitresult[Column.pv_index-1]);
						sr.setConditionMap(condition);
						String line[]=sr.getArrayFromResultSet();
						for(int j=0;j<line.length;j++)
						{	
							String splitline[] = line[j].split("\t");
							
							
							if(!TransactionMap.containsKey(property))
							{
								int k=checkCondition(splitline[Column.pv_index-1]);
								if(k==1)
								{
									GetDefaultvalue getDefaultvalue = new GetDefaultvalue(masterobject);
									String defaultvalue=getDefaultvalue.getdefaultvalue(splitline[Column.pid_index-1], splitline[Column.pv_index-1]);
									if(!defaultvalue.equalsIgnoreCase("error"))
										if(!TransactionMap.containsKey(property))
										TransactionMap.put(property, defaultvalue);
								}
								else if(k==2)
									if(!TransactionMap.containsKey(property))
									TransactionMap.put(property,splitline[Column.pv_index-1]);
							}
						}
					}
				}
			}
		} catch (Exception e) 
		{	
			e.printStackTrace();
		}
		masterobject.setHashtable(TransactionMap);
	}
	
	public void setObject(Object object) 
	{
		sr =new RecordSearch();
		this.masterobject =(Pool) object; 
		TransactionMap = masterobject.getHashtabel();
		gid =new GetIdName();
		TID = gid.getId(TID);
		TABLENAME = gid.getId(TABLENAME);
		TYPE = gid.getId(TYPE);
	}
	
	public int checkCondition(String conditionobject)
	{
		try 
		{
			RecordSearch rs =new RecordSearch();
			table.put("mid", conditionobject);
			rs.setConditionMap(table);
			String line[] = sr.getArrayFromResultSet();
			table.clear();
			for(int i=0;i<line.length;i++)
			{
				String splitline[]=line[i].split("\t");
				String value = splitline[Column.pv_index-1];
				if(value.equals("0"))
					value = splitline[Column.vt_index-1];
				else
					value = gid.getItem(value);
				table.put(gid.getItem(splitline[Column.pid_index-1]), value);
			}
			table = ConvertToLower.convertHashKey(table);
			table = ConvertToLower.convertHashKey(table);
            if(table.containsKey("class"))
                            return 1;
            
			/*try {
				if(!table.get("type").equals("condition"))
					return true;
			} catch (Exception e) 
			{
				return true;
			}*/
			String conditionvalue="";
			String conditionfield="";
			try {
				conditionvalue = table.get("conditionvalue").toString();
				conditionfield = table.get("conditionfield").toString();
			} catch (Exception e) 
			{
				return 1;
			}		
			if(TransactionMap.containsKey(conditionfield))
			{
				if(TransactionMap.get(conditionfield).equals(conditionvalue))
					return 1;
				else
					return 0;
			}
			if(property.containsKey(conditionfield))
			{
				if(property.get(conditionfield).equals(conditionvalue))
					return 1;
				else
					return 0;
			}
			return 0;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return 0;
		}
	}
	
	public Hashtable<Object, Object> getAllproperties() 
	{
		Hashtable<Object, Object> propertyMap = new Hashtable<Object, Object>();
		Enumeration<Object> enumoftransactiomap = TransactionMap.keys();
		while(enumoftransactiomap.hasMoreElements())
		{
			String key = enumoftransactiomap.nextElement().toString();
			//String value = TransactionMap.get(key).toString();
			if(propertyMap.containsKey(key))
			{
				int count = Integer.parseInt(propertyMap.get(key).toString());
				propertyMap.put(key,Integer.toString(++count));
			}
			else
				propertyMap.put(key,"1");
		}
		if(TransactionMap.containsKey("id"))
		{
			Hashtable<Object, Object> QueryMap = new Hashtable<Object, Object>();
			try 
			{	
				QueryMap.put("id", TransactionMap.get("id"));
				ResultSet rs = DBManager.getselect(QueryMap, "transaction");
				Vector<Object> column = DBManager.getSelect("select * from transaction", '1');
				while(rs.next())
				{
					for(int i=0;i<column.size();i++)
					{
						String key =rs.getString(column.get(i).toString());
						if(propertyMap.containsKey(key))
						{
							int count = Integer.parseInt(propertyMap.get(key).toString());
							propertyMap.put((key),Integer.toString(count++ ));
						}
						else							
							propertyMap.put((key),"1");
					}	
				}
			}
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
			QueryMap.clear();
			QueryMap.put("tid", TransactionMap.get("id"));
			sr.setConditionMap(QueryMap);
			String line[] = sr.getArrayFromResultSet();
			for(int i=0;i<line.length;i++)
			{
				String splitline[] =line[i].split("\t");
				if(propertyMap.containsKey(splitline[Column.pid_index-1]))
				{
					int count = Integer.parseInt(propertyMap.get(splitline[Column.pid_index-1]).toString());
					propertyMap.put(splitline[Column.pid_index-1],Integer.toString(count++ ));
				}
				else							
					propertyMap.put(splitline[Column.pid_index-1],"1");
			}
		}
		return propertyMap;
	}

	
	
}
